/** https://vjudge.net/problem/UVA-11902 */

#include<bits/stdc++.h>
using namespace std;
bool mat[106][106];
bool dom[106][106];
int nowi;
bool first[106];
bool after[106];
int n;

void dfs(int v, bool *vis) {
  if(vis[v] || v==nowi) return;
  vis[v] = true;
  for(int i=0;i<n;++i) {
    if(mat[v][i])dfs(i, vis);
  }
  return;
}

void solve() {
  cin>>n;

  string lace(2*n+1, '-');
  lace.front()=lace.back()='+';
  lace.push_back('\n');

  for(int i=0;i<n;++i) {
    for(int j=0;j<n;++j) {
      cin>>mat[i][j];
    }
  }

  dfs(0, first);

  for(int i=0;i<n;++i) {
    nowi = i;  // disable i
    memset(after, 0, sizeof(after));
    dfs(0, after);
    for(int j=0; j<n; ++j) {   // 注意j要从0开始, 因为后面的节点可能dominate前面的节点
      if(first[j] && !after[j]) {
        dom[i][j] = true;
      }
    }
  }

  cout << lace;
  for(int i=0; i<n; ++i) {
    cout<<'|';
    for(int j=0; j<n; ++j) {
      if(dom[i][j]) cout<<'Y';
      else cout<<'N';
      cout<<'|';
      if(j == n-1)cout<<'\n';
    }
    cout<<lace;
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  // freopen("tc/in.txt", "r", stdin);
  int tc;cin>>tc;
  for(int t=1; t<=tc; ++t) {
    nowi = -1;
    memset(mat, 0, sizeof(mat));
    memset(dom, 0, sizeof(dom));
    memset(first, 0, sizeof(first));
    memset(after, 0, sizeof(after));
    cout<<"Case "<<t<<":\n";
    solve();
  }
  return 0;
}
